// pages/wode/wode.js
const app=getApp()
//获得全局js变量
var util = require('../../utils/util.js');
Page({
	/**
	 * 页面的初始数据
	 */
	data: {
		imgurl:app.data.imgurl,
		signState:0,//签到状态
        userinfo: [],//用户信息
		userid:0
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
    var userId = app.globalData.userId;
    console.log(userId);
    if(userId == undefined || userId == 0 || userId == ''){
        wx.navigateTo({
          url: '/pages/auth/auth',
        })
    }
        this.setData({
            userid: app.globalData.userId,
            userinfo: app.globalData.userinfo
        });
	},
	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {
        //个人信心
        var userId = app.globalData.userId;
        console.log(userId);
        if(userId == undefined || userId == 0 || userId == ''){
           
        }else{
        	this.getUserInfoForMine();
        }
	},
    getUserInfoForMine:function(){
        var that=this;
        util.httpRrquest(app.data.rooturl + 'weixin/getUserInfo', 'GET',{})
        .then(function (response) {
            var data = response.data.data;
            if(data.loginStatus == 2){
                app.globalData.userId = data.id;
                app.globalData.userinfo = data;
                app.globalData.userPhone = data.userPhone;
                that.setData({
                	userid:data.id,
                	userinfo:data,
                	signState:data.sign
                });
            }
        }, function (error) {
            console.log(error);
        });
        
    },
	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function () {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function () {
		var shareinfo = app.setShareMessage();
        console.log(shareinfo);
        return {
            title: shareinfo.title,
            path: shareinfo.path,
            imageUrl: shareinfo.imageUrl,
            success: (res) => {
                wx.showToast({
                    title: "分享成功",
                    icon: 'none'
                })
            }
        }
	},
    //签到操作
	qiandao(){
        var that = this
		let state=this.data.signState
		var userid=this.data.userid;
		if(state==0){
            state=1;
            util.httpRrquest(app.data.rooturl + 'user/sign', 'GET', {userid:userid})
            .then(function (response) {
                
            }, function (error) {
                console.log(error);
            });
		}else{
			wx.showToast({
				title: '明天再来吧！',
				icon: 'none',
				duration: 2000
			})
		}
		this.setData({
			signState:state
		})
    },
    //拨打电话
	makePhoneCall(){
        var that=this
		wx.showModal({
            content: that.data.userinfo.kefu,
			confirmText:"拨打",
			success (res) {
			  if (res.confirm) {
				wx.makePhoneCall({
                    phoneNumber: that.data.userinfo.kefuphone
				})
			  } else if (res.cancel) {
				console.log('用户点击取消')
			  }
			}
		})
	}

})